namespace Digitalmes.Application.Mes;

/// <summary>
/// 物料产品信息表服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "mes")]
public sealed class MdItemService : IDynamicWebApiService
{
    private readonly SugarRepository<MdItem> _thisRepository;

    public MdItemService(SugarRepository<MdItem> thisRepository)
    {
        _thisRepository = thisRepository;
    }

    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<MdItemDto>> GetPagesAsync(MdItemPageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(!string.IsNullOrWhiteSpace(param.Key), s => s.ItemCode.Contains(param.Key) || s.ItemName.Contains(param.Key))
            .WhereIF(param.Classify > 0, s => (int)s.Classify == param.Classify)
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<MdItemDto>>();
    }

    /// <summary>
    /// 通过关键字查询产品
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<MdItemDto>> GetPagesByKeywordAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
             .WhereIF(!string.IsNullOrWhiteSpace(param.Key), s => s.ItemCode.Contains(param.Key) || s.ItemName.Contains(param.Key))
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<MdItemDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<MdItemDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<MdItemDto>();
    }

    /// <summary>
    /// 获取所有的成品
    /// </summary>
    /// <returns></returns>
    public async Task<List<MdItemDto>> GetAllProductAsync()
    {
        var items = await _thisRepository.GetListAsync(s => s.Classify == MaterialClassifyEnum.Finished);
        return items.Adapt<List<MdItemDto>>();
    }

    /// <summary>
    /// 通过分类获取所有的产品
    /// </summary>
    /// <param name="classify">产品分类</param>
    /// <returns></returns>
    public async Task<List<MdItemDto>> GetListByClassifyAsync(MaterialClassifyEnum classify)
    {
        var items = await _thisRepository.GetListAsync(s => s.Classify == classify);
        return items.Adapt<List<MdItemDto>>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync([FromBody] MdItemDto model)
    {
        if (await _thisRepository.IsAnyAsync(s => s.ItemCode == model.ItemCode))
        {
            Oops.ThrowBah(ErrCode.E4002);
        }

        return await _thisRepository.InsertAsync(model.Adapt<MdItem>());
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync([FromBody] MdItemDto model)
    {
        if (await _thisRepository.IsAnyAsync(s => s.ItemCode == model.ItemCode && s.Id != model.Id))
        {
            Oops.ThrowBah(ErrCode.E4002);
        }

        return await _thisRepository.UpdateAsync(model.Adapt<MdItem>());
    }

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody] List<long> ids) =>
        await _thisRepository.DeleteAsync(m => ids.Contains(m.Id));
}
